<?php

namespace App\Http\Controllers\Admin\Stock;

use App\Http\Controllers\Admin\BaseController;
use App\Repositories\MemberRepositoryEloquent;
use App\User;
use App\Models\Recharge;
use Breadcrumbs, Toastr,Config,Validator,DB;
use Illuminate\Http\Request;
use App\Models\StockOrder;

class StockOrderController extends BaseController{

    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;
    }

    public function index(Request $request){
        $where=[];
        $whereHas=[];
        $data=['status'=>-1,'name'=>'','type'=>'','mobile'=>'','from'=>'','to'=>''];

        if(!empty($request->all())){
            //if($request->name){
            //dd($request->all());
            $datas = $request->all();
            foreach($datas as $k=>$v){
                if(($k=='name' || $k=='mobile') && $v!=''){
                    $whereHas[] = ["$k",'like','%'.$v.'%'];
                }
                if($k=='from' && $v!=''){
                    //$where[] = ['created_at','>',strtotime($v)];
                    $where[] = ['created_at','>',$v];
                }
                if($k=='to' && $v!=''){
                    //$where[] = ["created_at",'<',strtotime($v)];
                    $where[] = ["created_at",'<',$v];
                }
                if($k=='status' && $v!='' && $v!=-1){
                    $where[] = ["$k",'=',$v];
                }
                $data[$k] =$v;
                
            }
            //dd($where);
            //$users = User::where($where)->paginate(20);
        }
        //$activities = [];
        $orders = StockOrder::where($where)
                        ->whereHas('user',function($query) use($whereHas){
                            $query->where($whereHas);
                        })->orderBy('id','desc')->paginate(20);
        return view('admin.stock.orders',compact('orders','data'));
    }

    /**
     * [back 提现退回]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function back($id){
        $order = StockOrder::where('id','=',$id)->where('status','=',1)->first();
        if(!$order){ 
            return response()->json(['status' => -1,'msg'=>'没有相关提现记录！']);
        }
        
        if($order->activity->status==2){ 
            return response()->json(['status' => -1,'msg'=>'活动已结算,无法退回！']);
        }

        DB::beginTransaction();
        try{
            $result = null;
            $res = StockOrder::where('id','=',$id)->where('status','=',1)->update(['status'=>2]);
            if($res){

                $user = User::find($order->user_id);
                $backMoney = $order->pay_money;
                $note = "股票[".$order->stock_name."]订单退回";
                $result = $result = $this->user->modifyBalance($user->id,$backMoney,5,$note);
            }
            DB::commit(); 
            return response()->json($result ? ['status' => 1] : ['status' => -1,'msg'=>'返回资金失败']);
        }catch(\Exception $e){
            //接收异常处理并回滚 
            DB::rollBack(); 
            return response()->json(['status' => -1,'msg'=>'处理数据异常！']);
        }
    } 

}